import {createRouter, createWebHashHistory, RouteRecordRaw} from 'vue-router'

const routes:Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: "/home"
  },
  {
    path: '/home',
    name: 'home',
    component: () => import("../views/index.vue"),
    children: [
      {
        path: "/home",
        redirect: "/home/user"
      },
      {
        path: 'user',
        name: "user",
        component: () => import("../views/user/user.vue")
      },
      {
        path: 'allCartoon',
        name: "allCartoon",
        component: () => import("../views/cartoon/allCartoon.vue")
      },
      {
        path: 'newGrounding',
        name: "newGrounding",
        component: () => import("../views/cartoon/newGrounding.vue")
      },
      {
        path: 'todayRecommend',
        name: "todayRecommend",
        component: () => import("../views/cartoon/todayRecommend.vue")
      },
      {
        path: 'swiperManage',
        name: "swiperManage",
        component: () => import("../views/cartoon/swiperManage.vue")
      },
      {
        path: 'weekRecommend',
        name: "weekRecommend",
        component: () => import("../views/cartoon/weekRecommend.vue")
      },
      {
        path: 'essay',
        name: "essay",
        component: () => import("../views/essay/essay.vue")
      },
      {
        path: 'essayComment/:id',
        name: "essayComment",
        component: () => import("../views/essay/essayComment.vue")
      },
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import("../views/login/login.vue")
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to,from,next) => {
  // 路由拦截
  if(to.path != "/login"){
    if(sessionStorage.getItem('token') == "18944155125"){
      next()
    }else{
      from.path // 防止报错写的
      next('/login')
    }
  }else{
    next()
  }
})

export default router
